﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using DotNet.Framework.Common.Algorithm;

//using DotNet.Framework.Common.Algorithm;

namespace OCR.Digital.Comm
{
    public class Compar
    {
        private string code01;
        SortedList sl;
        public Compar(string code01)
        {
            this.code01 = code01;
            sl = new SortedList();
        }
        //用的别人的DLL文件解析
        public string GetCode()
        {
            var ld = new LevenshteinDistance();
            DataTable dt = GetDataTable();
            string value = string.Empty;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                value = dt.Rows[i][1].ToString();
                decimal parent = ld.LevenshteinDistancePercent(value, code01);
                if (parent > 0.85m)
                {
                    if (!sl.ContainsKey(parent))
                    {
                        sl.Add(parent, dt.Rows[i][0]);
                    }
                }
            }
            if (sl.Count > 0)
                return sl.GetByIndex(sl.Count - 1).ToString();
            else return "";
        }
        //自己写的简单方法
        public string GetCode2()
        {
            var ld = new LevenshteinDistance();
            DataTable dt = GetDataTable();
            string value = string.Empty;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                value = dt.Rows[i][1].ToString();
                double parent = MatchCharCount(value, code01);
                if (parent > 0.85d)
                {
                    if (!sl.ContainsKey(parent))
                    {
                        sl.Add(parent, dt.Rows[i][0]);
                    }
                }
            }
            if (sl.Count > 0)
                return sl.GetByIndex(sl.Count - 1).ToString();
            else return "";
        }
        /// <summary>
        /// 最简单的匹配方法
        /// </summary>
        /// <param name="data"></param>
        /// <param name="match"></param>
        /// <returns></returns>
        public double MatchCharCount(string data, string match)
        {
            var returnValue = 0d;
            char[] dataCharArray = data.ToCharArray();
            char[] matchCharArray = match.ToCharArray();
            int length = dataCharArray.Length <= matchCharArray.Length ? dataCharArray.Length : matchCharArray.Length;
            for (int i = 0; i < length; i++)
            {
                if (dataCharArray[i] == matchCharArray[i])
                    returnValue++;
            }
            return returnValue / length;
        }

        public DataTable GetDataTable()
        {
            DataTable dt = new DataTable("tbcode");
            dt.Columns.Add("value");
            dt.Columns.Add("codes");
            //读取文件
            string path = System.Environment.CurrentDirectory;
            path = Regex.Replace(path, @"\\bin\\Debug", "") + "\\code.txt";
            using (StreamReader sr = new StreamReader(path, Encoding.Default))
            {
                String line;
                while ((line = sr.ReadLine()) != null)
                {
                    string[] strs = line.Split(':');
                    if (strs.Length >= 2)
                    {
                        DataRow dr = dt.NewRow();
                        dr["value"] = strs[0];
                        dr["codes"] = strs[1];
                        dt.Rows.Add(dr);
                    }
                }
            }
            return dt;
        }
    }
}
